﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Model;
using GridView = Ext.Net.GridView;
using Model = Ext.Net.Model;

public partial class Customers : System.Web.UI.Page
{
     
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Cookies.getCookies("uid") == "0")
        {
            Response.Redirect("~/Login.aspx");
        }
        if (X.IsAjaxRequest)
        {
            if (Request["s"] != null)
            {
                BtnBind(int.Parse(Request["s"]));
            }
           
            //We do not need to DataBind on an DirectEvent
            using (var m = new ACEntities())
            {
                var StoreBigClass = from c in m.Catalogs
                                    where c.ParentID == 0
                                    select c;
                StoreBC.DataSource = StoreBigClass;
                StoreBC.DataBind();
                if (Request["bid"] != null)
                {
                    X.AddScript("#{StoreSC}.reload();");
                    ComboBox2.SetValue(Request["bid"]);
                }
                StoreBC1.DataSource = StoreBigClass;
                StoreBC1.DataBind();
                
            }
            return;
            
        }
    }

    protected void StoreSC_onRefresh(object sender,StoreReadDataEventArgs e)
    {
        int pid = int.Parse(ComboBox1.SelectedItem.Value);
        using (var m = new ACEntities())
        {
            var StoreSmallClass = from c in m.Catalogs
                                  where c.ParentID == pid
                                  select c;
            StoreSC.DataSource = StoreSmallClass;
            StoreSC.DataBind();
        }
    }

    protected void StoreSC1_onRefresh(object sender, StoreReadDataEventArgs e)
    {
        int pid = int.Parse(BigClassID.SelectedItem.Value);
        using (var m = new ACEntities())
        {
            var StoreSmallClass = from c in m.Catalogs
                                  where c.ParentID == pid
                                  select c;
            StoreSC1.DataSource = StoreSmallClass;
            StoreSC1.DataBind();
        }
    }

    [DirectMethod]
    public static string GetGrid(Dictionary<string, string> parameters)
    {
        int ctmid = int.Parse(parameters["id"]);
        using (var m = new ACEntities())
        {
            //long LID = long.Parse(id);
            //int uid = int.Parse(Cookies.getCookies("uid"));
            //var q = from c in m.VWCustomers
            //        where c.CTMID == LID && c.isStatus == false && c.LastUserID == uid
            //        orderby c.inTime descending
            //        select c;
            var q = from c in m.VWCustomers
                    where c.CTMID == ctmid && c.isStatus == false && c.LastUserID != null
                    orderby c.inTime descending
                    select c;
            GridPanel grid = new GridPanel
                {
                    Height = 200,
                    EnableColumnHide = false,
                    Store =
                        {
                            new Store
                                {
                                    Model =
                                        {
                                            new Ext.Net.Model
                                                {
                                                    IDProperty = "ID",
                                                    Fields =
                                                        {
                                                            new ModelField("ID"),
                                                            new ModelField("CTMID"),
                                                            new ModelField("BigClassID"),
                                                            new ModelField("SmallClassID"),
                                                            new ModelField("CTMCompany"),
                                                            new ModelField("CTMUsername"),
                                                            new ModelField("CTMPhone"),
                                                            new ModelField("CTMFax"),
                                                            new ModelField("CTMEmail"),
                                                            new ModelField("CTMAddress"),
                                                            new ModelField("LastTime", ModelFieldType.Date),
                                                            new ModelField("LastUserID"),
                                                            new ModelField("LastStatus"),
                                                            new ModelField("LastUserID"),
                                                            new ModelField("LastStatus"),
                                                            new ModelField("LastRemarks"),
                                                            new ModelField("NextContectTime"),
                                                            new ModelField("isStatus"),
                                                            new ModelField("inTime", ModelFieldType.Date),
                                                            new ModelField("Status"),
                                                            new ModelField("BigClass"),
                                                            new ModelField("username"),
                                                            new ModelField("SmallClass") 
                                                        }
                                                }
                                        },
                                    DataSource = q
                                }
                        },
                    ColumnModel =
                        {
                            Columns =
                                {  
                                    //new Column {Text = "大類", DataIndex = "BigClass"},
                                    //new Column {Text = "小類", DataIndex = "SmallClass"},
                                    new Column {Text = "公司", DataIndex = "CTMCompany" },
                                    new Column {Text = "姓名", DataIndex = "CTMUsername"},
                                    new Column {Text = "电邮", DataIndex = "CTMEmail"},
                                    new Column {Text = "電話", DataIndex = "CTMPhone"},
                                    new Column {Text = "傳真", DataIndex = "CTMFax"},
                                    new Column {Text = "地點", DataIndex = "CTMAddress"},
                                    new DateColumn {Text = "上次日期", DataIndex = "LastTime",Format = "yyyy-MM-dd"},
                                    new Column {Text = "員工", DataIndex = "username"},
                                    new Column {Text = "上次情況", DataIndex = "Status"},
                                    new Column {Text = "內容", DataIndex = "LastRemarks",Flex = 1},
                                    new DateColumn {Text = "提醒日期", DataIndex = "NextContectTime",Format = "yyyy-MM-dd"},
                                    new DateColumn {Text = "时间", DataIndex = "inTime",Format = "yyyy-MM-dd HH:mm"}
                                }
                        } 
                        
                       
                };

            return ComponentLoader.ToConfig(grid);
        }
    }

    protected void RecorderOperates(object sender, DirectEventArgs e)
    {
        int id = int.Parse(e.ExtraParams["id"].ToString());
        string cmd = e.ExtraParams["cmd"].ToString();
        int uid = int.Parse(Cookies.getCookies("uid"));
        using (var m = new ACEntities())
        {
            var q = (from c in m.Customers
                         where c.CTMID == id && c.isStatus == true
                         select c).FirstOrDefault();
            switch (cmd)
            {
                case "BuYao":
                    var c1 = new global::Model.Customers();
                    c1.CTMID = q.CTMID;
                    c1.BigClassID = q.BigClassID;
                    c1.SmallClassID = q.SmallClassID;
                    c1.CTMCompany = q.CTMCompany;
                    c1.CTMUsername = q.CTMUsername;
                    c1.CTMPhone = q.CTMPhone;
                    c1.CTMFax = q.CTMFax;
                    c1.CTMEmail = q.CTMEmail;
                    c1.CTMAddress = q.CTMAddress;
                    c1.LastTime = DateTime.Now;
                    c1.LastUserID = uid;
                    c1.LastStatus = 2;
                    c1.LastRemarks = "";
                    c1.NextContectTime = null;
                    c1.isStatus = true;
                    c1.inTime = DateTime.Now;
                    m.AddToCustomers(c1);
                    q.isStatus = false; 
                     m.SaveChanges();
                     X.AddScript("#{GridPanel1}.store.load();");
                    break;
                    
                case "WeiJie":
                    var c2 = new global::Model.Customers();
                    c2.CTMID = q.CTMID;
                    c2.BigClassID = q.BigClassID;
                    c2.SmallClassID = q.SmallClassID;
                    c2.CTMCompany = q.CTMCompany;
                    c2.CTMUsername = q.CTMUsername;
                    c2.CTMPhone = q.CTMPhone;
                    c2.CTMFax = q.CTMFax;
                    c2.CTMEmail = q.CTMEmail;
                    c2.CTMAddress = q.CTMAddress;
                    c2.LastTime = DateTime.Now;
                    c2.LastUserID = uid;
                    c2.LastStatus = 1;
                    c2.LastRemarks = "";
                    c2.NextContectTime = null;
                    c2.isStatus = true;
                    c2.inTime = DateTime.Now;
                    m.AddToCustomers(c2);
                    q.isStatus = false; 
                     m.SaveChanges();
                    X.AddScript("#{GridPanel1}.store.load();");
                    break;
                case "GenJin":
                    CTMID.Clear();
                    BigClassID.Clear();
                    SmallClassID.Clear();
                    CTMCompany.Clear();
                    CTMUsername.Clear();
                    CTMPhone.Clear();
                    CTMFax.Clear();
                    CTMEmail.Clear();
                    CTMAddress.Clear();
                    CTMNextContectTime.Value = DateTime.Now;
                    CTMID.Text=q.CTMID.ToString();
                    BigClassID.SetValue(q.BigClassID);
                    
                    CTMCompany.SetValue(q.CTMCompany.ToString());
                    CTMUsername.SetValue(q.CTMUsername);
                    CTMPhone.SetValue(q.CTMPhone);
                    CTMFax.SetValue(q.CTMFax.ToString());
                    CTMEmail.SetValue(q.CTMEmail);
                    CTMAddress.SetValue(q.CTMAddress); 
                    Window1.Show();
                    X.AddScript("#{StoreSC1}.reload();");
                    SmallClassID.SetValue(q.SmallClassID);
                    break;
                default:
                    break;
            } 
        } 
    }

    protected void BindGP(object sender, DirectEventArgs e)
    {
        int s = int.Parse(e.ExtraParams["s"]);

        int bid = 0;
        int sid = 0;
        string url = "~/Customers.aspx?s=" + s;
        if (s == 5)
        { 
            if (ComboBox1.SelectedItem.Index != -1)
            {
                url = url + "&bid=" + ComboBox1.SelectedItem.Value;
            }
            //X.Msg.Alert("AA",bid.ToString()).Show();
            if (ComboBox2.SelectedItem.Index != -1)
            {
                url = url + "&sid=" + ComboBox2.SelectedItem.Value;
            }
        }

        Response.Redirect(url); 
    }

    protected void SaveGenJin(object sender, DirectEventArgs e)
    {
        int ctmid = int.Parse(CTMID.Text);
        int uid = int.Parse(Cookies.getCookies("uid"));
        using (var m = new ACEntities())
        {
            var q = (from c in m.Customers
                     where c.CTMID == ctmid && c.isStatus == true 
                     select c).FirstOrDefault();
            var c1 = new global::Model.Customers();
            c1.CTMID = q.CTMID;
            c1.BigClassID = int.Parse(BigClassID.SelectedItem.Value);
            c1.SmallClassID = int.Parse(SmallClassID.SelectedItem.Value);
            c1.CTMCompany = CTMCompany.Text;
            c1.CTMUsername = CTMUsername.Text;
            c1.CTMPhone = CTMPhone.Text;
            c1.CTMFax = CTMFax.Text;
            c1.CTMEmail = CTMEmail.Text;
            c1.CTMAddress = CTMAddress.Text;
            c1.LastTime = DateTime.Now;
            c1.LastUserID = uid;
            c1.LastStatus = int.Parse(CTMLastStatus.SelectedItem.Value);
            c1.LastRemarks = CTMLastRemarks.Text;
             //X.Msg.Alert("AA",CTMNextContectTime.Value.ToString()).Show();
            if (!CTMNextContectTime.Value.ToString().StartsWith("0"))
            {
                c1.NextContectTime = DateTime.Parse(CTMNextContectTime.Value.ToString());
            }
            
            c1.inTime = DateTime.Now;
            c1.isStatus = true;
            m.AddToCustomers(c1);
            q.isStatus = false;
            m.SaveChanges();
            X.AddScript("#{GridPanel1}.store.load();");
            Window1.Hide();
        }
    }

    protected void BtnBind(int s)
    {
        Button1.Disabled = false;
        Button2.Disabled = false;
        Button3.Disabled = false;
        Button4.Disabled = false;
        Button5.Disabled = false;
        Button8.Disabled = false;

        switch (s)
        {
            case 1: //沒接
                Button1.Disabled = true;
                //X.Msg.Notify("提醒", "当前显示“没接”客户！").Show();
                break;
            case 2: //不要
                Button2.Disabled = true;
                //X.Msg.Notify("提醒", "当前显示“不要”客户！").Show();
                break;
            case 3: //跟進
                Button3.Disabled = true;
                //X.Msg.Notify("提醒", "当前显示跟进客户！").Show();
                break;
            case 4: //未处理
                Button4.Disabled = true;
                //X.Msg.Notify("提醒", "当前显示未处理客户").Show();
                break;
            case 5: //抽取
                Button4.Disabled = true;

                //X.Msg.Notify("提醒", "已经抽取100条未处理客户！").Show();
                break;
            case 6: //全部
                Button1.Disabled = false;
                Button2.Disabled = false;
                Button3.Disabled = false;
                Button4.Disabled = false;
                Button5.Disabled = false;
                //X.Msg.Notify("提醒", "当前显示全部客户！").Show();
                break;
            case 7:
                Button8.Disabled = true;
                break;
            default:
                break;
        }
    }

    protected void Store1_Refresh(object sender, StoreReadDataEventArgs e)
    {
        int start = int.Parse(e.Parameters["startRemote"]);
        int limit = int.Parse(e.Parameters["limitRemote"]);
        int s = 0;
        int uid = int.Parse(Cookies.getCookies("uid"));
        if (Request["s"] != null)
        {
            s = int.Parse(Request["s"]);
        }
        
        using (var m = new ACEntities())
        {
            Store store = this.GridPanel1.GetStore();
            int result = 0;
            switch (s)
            {
                case 1: //沒接
                    var q1 = (from c in m.VWCustomers
                              where c.LastStatus == s && c.LastUserID == uid && c.isStatus == true
                              orderby c.ID descending
                              select c).Skip(start).Take(limit);
                    result = (from c in m.VWCustomers
                              where c.LastStatus == s && c.LastUserID == uid && c.isStatus == true
                              select c).Count();
                    //Slider1.MaxValue = result;
                    store.DataSource = q1;
                    break;
                case 2: //不要

                    var q2 = (from c in m.VWCustomers
                             where c.LastStatus == s && c.LastUserID == uid && c.isStatus == true
                             orderby c.ID descending 
                             select c).Skip(start).Take(limit);
                    result = (from c in m.VWCustomers
                              where c.LastStatus == s && c.LastUserID == uid && c.isStatus == true
                              select c).Count();
                    //Slider1.MaxValue = result;
                    store.DataSource = q2;
                    break;
                case 3: //跟進
                    DateTime dt = DateTime.Now.AddDays(-1);
                    var q3 = from c in m.VWCustomers
                             where c.LastStatus == s && c.LastUserID == uid && c.isStatus == true
                             where c.NextContectTime>=dt
                             orderby c.NextContectTime ascending 
                             select c;
                    result = (from c in m.VWCustomers
                              where c.LastStatus == s && c.LastUserID == uid && c.isStatus == true
                              select c).Count();
                    //Slider1.MaxValue = result;
                    store.DataSource = q3;
                    break;
                case 4: //未处理
                    var q4 = (from c in m.VWCustomers
                             where c.LastStatus == s && c.LastUserID == uid && c.isStatus == true
                             orderby c.LastTime descending 
                             select c).Skip(start).Take(limit);;
                    result = (from c in m.VWCustomers
                              where c.LastStatus == s && c.LastUserID == uid && c.isStatus == true
                              select c).Count();
                    //Slider1.MaxValue = result;
                    store.DataSource = q4;
                    break;
                case 5: //抽取 
                    int bid = 0;
                    int sid = 0;
                    if (Request["bid"] != null)
                    {
                        bid = int.Parse(Request["bid"]); 
                    }
                    if(Request["sid"]!=null)
                    {
                        sid = int.Parse(Request["sid"]);
                    }
                    if(sid!=0)
                    {
                        var q5 = (from c in m.VWCustomers
                                  where c.LastStatus == 4 && c.LastUserID == null && c.isStatus == false && c.SmallClassID==sid
                                  select c).Take(200);
                        foreach (var w in q5)
                        {
                            var q = (from c in m.Customers
                                     where c.ID == w.ID
                                     select c).FirstOrDefault();
                            q.LastUserID = uid;
                            q.LastTime = DateTime.Now; 
                            q.isStatus = true;
                        }
                        m.SaveChanges();

                        string url = "~/Customers.aspx?s=4";

                        X.Redirect(url);
                    }
                    else
                    {
                        if(bid!=0)
                        {
                            var q5 = (from c in m.VWCustomers
                                      where c.LastStatus == 4 && c.LastUserID == null && c.isStatus == false && c.BigClassID==bid
                                      select c).Take(200);
                            foreach (var w in q5)
                            {
                                var q = (from c in m.Customers
                                         where c.ID == w.ID
                                         select c).FirstOrDefault();
                                q.LastUserID = uid;
                                q.LastTime = DateTime.Now; 
                                q.isStatus = true;
                            }
                            m.SaveChanges();

                            string url = "~/Customers.aspx?s=4";

                            X.Redirect(url);
                        }
                        else
                        {
                            var q5 = (from c in m.VWCustomers
                                      where c.LastStatus == 4 && c.LastUserID == null && c.isStatus == false
                                      select c).Take(200);
                            foreach (var w in q5)
                            {
                                var q = (from c in m.Customers
                                         where c.ID == w.ID
                                         select c).FirstOrDefault();
                                q.LastUserID = uid;
                                q.LastTime = DateTime.Now; 
                                q.isStatus = true;
                            }
                            m.SaveChanges();

                            string url = "~/Customers.aspx?s=4";

                            X.Redirect(url);
                        }
                    }
                    
                    break;
                case 6: //全部
                    var q6 = (from c in m.VWCustomers
                              where c.LastUserID == uid && c.isStatus==true
                              orderby c.ID descending 
                              select c).Skip(start).Take(limit);
                     result = (from c in m.VWCustomers
                                  where c.LastUserID == uid 
                                  select c).Count();
                    //Slider1.MaxValue = result;
                    
                    store.DataSource = q6;
                    break;
                case 7: //确实
                    var q7 = (from c in m.VWCustomers
                              where c.LastUserID == uid && c.isStatus == true && c.LastStatus==5
                              orderby c.ID descending
                              select c).Skip(start).Take(limit);
                    result = (from c in m.VWCustomers
                              where c.LastUserID == uid
                              select c).Count();
                    //Slider1.MaxValue = result;

                    store.DataSource = q7;
                    break;
                case 8://检索
                    int mm = int.Parse(Request["m"]);
                    string k = Request["k"];
                    CMDSearch.Select(mm-1);
                    TFKey.Text = k;
                    switch (mm)
                    {
                        case 1://电话
                            var q81 = (from c in m.VWCustomers
                              where c.LastUserID == uid && c.isStatus==true && c.CTMPhone.Contains(k)
                              orderby c.ID descending 
                              select c).Skip(start).Take(limit);
                                result = (from c in m.VWCustomers
                                            where c.LastUserID == uid 
                                            select c).Count();
                            //Slider1.MaxValue = result;
                    
                            store.DataSource = q81;
                            break;
                        case 2://传真
                             var q82 = (from c in m.VWCustomers
                              where c.LastUserID == uid && c.isStatus==true && c.CTMFax.Contains(k)
                              orderby c.ID descending 
                              select c).Skip(start).Take(limit);
                                result = (from c in m.VWCustomers
                                            where c.LastUserID == uid 
                                            select c).Count();
                            //Slider1.MaxValue = result;
                    
                            store.DataSource = q82;

                            break;
                        case 3://公司
                             var q83 = (from c in m.VWCustomers
                              where c.LastUserID == uid && c.isStatus==true && c.CTMCompany.Contains(k)
                              orderby c.ID descending 
                              select c).Skip(start).Take(limit);
                                result = (from c in m.VWCustomers
                                            where c.LastUserID == uid 
                                            select c).Count();
                            //Slider1.MaxValue = result;
                    
                            store.DataSource = q83;
                            break;
                        case 4://号码
                            int ctmid = int.Parse(k);
                            var q84 = (from c in m.VWCustomers
                                       where c.LastUserID == uid && c.isStatus == true && c.CTMID ==ctmid 
                            
                              orderby c.ID descending 
                              select c).Skip(start).Take(limit);
                                result = (from c in m.VWCustomers
                                            where c.LastUserID == uid 
                                            select c).Count();
                            //Slider1.MaxValue = result;
                    
                            store.DataSource = q84;
                            break;
                            default:
                            break;
                    }
                    break;
                default:
                    break;
            }
            store.DataBind();
        }
        
         
    }

    protected void PageClick(object sender, DirectEventArgs e)
    {
        int page = int.Parse(pageindex.Text);
        int method = int.Parse(e.ExtraParams["s"]);
        page += method;
        if (page <= 0)
        {
            page = 0;
        }
        
        pageindex.Text = page.ToString();

    }

    protected void SearchClick(object sender, DirectEventArgs e)
    {
        Response.Redirect("~/Customers.aspx?s=8&m=" + CMDSearch.SelectedItem.Value+"&k="+TFKey.Text);
    }
}